package security.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import security.model.Dashboard;
import security.model.Organization;



@Stateless
public class DashboardDAO {
	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	public Dashboard getById(Integer id){	
		return em.find(Dashboard.class, id);
	}
	
	public Dashboard save(Dashboard dashboard) {
    	em.persist(dashboard);
    	return dashboard;
    }
	
    public Dashboard update(Dashboard dashboard) {
    	return em.merge(dashboard);
    }
    
    public void delete(Integer id) {
    	em.remove(em.find(Dashboard.class, id));
    }
	
	@SuppressWarnings("unchecked")
    public List<Dashboard> listByOrganization(Organization organization) {	
    	Query query = em.createQuery("SELECT e FROM Dashboard e  WHERE e.organization=:organization");
    	query.setParameter("organization", organization);
        return query.getResultList();   
    }



}

